﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Text;
using System.Threading;
using JinianNet.JNTemplate;

namespace codeGenerator.model
{
    public class TableProfile
    {
        public string TableName { get; set; }
        public List<ColumnMeta> TableMetas { get; set; }

        public TableProfile()
        {
        }

        public TableProfile(string tableName, List<ColumnMeta> tableMetas)
        {
            TableName = tableName;
            TableMetas = tableMetas;
        }

        /**
         * sql转换javabean
         */
        public String Sql2JavaBean()
        {
            string tempPath = Environment.CurrentDirectory + Path.DirectorySeparatorChar + "Resources" +
                              Path.DirectorySeparatorChar + "javaBean.txt";
            ITemplate template = Engine.LoadTemplate(tempPath);
            // 获取javaBean模板
            // ITemplate template = Engine.CreateTemplate(TemplateFactory.getJavaBeanTemp().TempStr);
            // 表名转换类名
            var tableName = TableMetas[0].TableName;
            string[] tableNameSplit = tableName.Split('_');
            StringBuilder className = new StringBuilder();
            foreach (var nSplit in tableNameSplit)
            {
                className.Append(CultureInfo.CurrentCulture.TextInfo.ToTitleCase(nSplit.ToLower()));
            }

            // 配置get set 成员列名下划线转换驼峰 成员变量由数据库类型转换为Java类型
            // 增加表的默认字段 创建时间,修改时间,创建人,修改人_set
            foreach (var columnMeta in TableMetas)
            {
                columnMeta.ColumnType = SqlColumnType2JavaBeanType(columnMeta.ColumnType);
                string lineToHump = LineToHump(columnMeta.ColumnName);
                columnMeta.ColumnName = lineToHump;
            }

            template.Set("className", className);
            template.Set("columns", TableMetas);
            template.Set("now", DateTime.Now);
            return template.Render();
        }

        /**
         * oracle类型转换java类型
         */
        public String SqlColumnType2JavaBeanType(String columnType)
        {
            // List<string> dateTypes = new List<String>();
            List<string> numberTypes = new List<String>();
            numberTypes.Add("long");
            numberTypes.Add("number");
            numberTypes.Add("integer");
            numberTypes.Add("decimal");

            if (numberTypes.Contains(columnType))
            {
                if (columnType == "decimal" || columnType == "real" || columnType == "float")
                {
                    return "Double";
                }

                return "Long";
            }

            if (columnType == "date" || columnType == "timestamp")
            {
                return "Date";
            }

            return "String";
        }

        /**
         * 带"_"字段名 转换小驼峰  create_date -> createDate
         */
        static string LineToHump(string name)
        {
            StringBuilder builder = new StringBuilder();
            foreach (var s in name.Split(new[] {'_'}, StringSplitOptions.RemoveEmptyEntries))
            {
                if (builder.Length > 0)
                {
                    builder.Append(Thread.CurrentThread.CurrentCulture.TextInfo.ToTitleCase(s));
                }
                else
                {
                    builder.Append(s);
                }
            }

            return builder.ToString();
        }
    }
}